/* 
 * File:   commons.h
 * Author: Administrator
 *
 * Created on 2013?8?23?, ??10:22
 */

#ifndef COMMONS_H
#define	COMMONS_H

#ifdef	__cplusplus
extern "C" {
#endif




#ifdef	__cplusplus
}
#endif

#include "p30f6010A.h"
#include <spi.h>
#include <pwm.h>
#include <ports.h>
#include <stdlib.h>
#include <stdio.h>
#include <timer.h>
#include <uart.h>
#include <xlcd.h>
#include <time.h>
#include <math.h>
#include <string.h>






#define PCBGen "2Gen"
#define FCY  8000000				
#define MILLISEC FCY/8000			
#define uSEC FCY/8000/1000

#define BAUDRATE 9600
#define TMR_RATE	FCY/64			
#define HALF_SEC	TMR_RATE/2		
#define SEC	TMR_RATE



#define S1 !PORTDbits.RD8
#define S2 !PORTDbits.RD10
#define S3 !PORTDbits.RD12
#define S4 !PORTDbits.RD9
#define S5 !PORTDbits.RD11



#define Out1 PORTBbits.RB15	
#define Out2 PORTBbits.RB14	
#define Out3 PORTBbits.RB13	
#define Out4 PORTBbits.RB12	
#define Out5 PORTBbits.RB8	
#define Out6 PORTBbits.RB9	
#define Out7 PORTBbits.RB10	
#define Out8 PORTBbits.RB11	



#define LickOnPin        PORTAbits.RA9 





#define RS	LATFbits.LATF8 
#define RW	LATFbits.LATF2 
#define E	LATFbits.LATF3

#define DATA	LATD
#define TRIS_DATA  TRISD

#define  RW_TRIS	TRISFbits.TRISF2
#define  RS_TRIS	TRISFbits.TRISF8
#define  E_TRIS		TRISFbits.TRISF3


#define set_function()	lcd_cmd( 0x38 ) 
#define cursor_right()  lcd_cmd( 0x14 )
#define cursor_left()   lcd_cmd( 0x10 )
#define display_shift() lcd_cmd( 0x1C )
#define home_clr()      lcd_cmd( 0x01 ) 
#define home_it()       lcd_cmd( 0x02 ) 
#define line_2()        lcd_cmd( 0xC0 ) 

#define TRUE 1			
#define FALSE 0


#define water  3  
#define dry    4  
#define beep1  2
#define beep2  1




#define  water_sweet    1    
#define  odor_A         2    

#define  odor_B         3    
#define  water_bitter   4    
#define  odor_C			5	 
#define  odor_D			6	 
#define  odor_E			7	 
#define  odor_F			8	 
#define  odor_G			9	 
#define  odor_H			10	 
#define  odor_I			11	 
#define  odor_J			12	 
#define  odor_K			13	 
#define  odor_L			14	 
#define  odor_M			15	 
#define  odor_N			16	 


#define SpLick 	 0 	
#define SpCome 	 1  	
#define SpLick2	 2   
#define SpCome2	 3   	

#define SpFalseAlarm		 4	
#define SpCorrectRejection		 5	
#define SpMiss		 6	
#define SpHit		 7 	

#define SpWater_sweet	 8     
#define SpOdor_A     9         
#define SpOdor_B	10    
#define SpWater_bitter	 11   

#define SpValve5	12
#define SpValve6 	13
#define SpValve7 	14
#define SpValve8 	15  



#define SpOut1		16  
#define SpOut2	 	17  
#define SpOut3	 	18  
#define SpOut4		19 



#define SpS1S1 	 20
#define SpS1S2   21
#define SpS1S3   22
#define SpS1S4	 23
#define SpS1S5   24
#define SpS2S1 	 25
#define SpS2S2   26
#define SpS2S3   27
#define SpS2S4	 28
#define SpS2S5   29
#define SpS3S1 	 30
#define SpS3S2   31
#define SpS3S3   32
#define SpS3S4	 33
#define SpS3S5   34
#define SpS4S1 	 35
#define SpS4S2   36
#define SpS4S3   37
#define SpS4S4	 38
#define SpS4S5   39
#define SpS5S1 	 40
#define SpS5S2   41
#define SpS5S3   42
#define SpS5S4	 43
#define SpS5S5   44


#define SpStepN         51
#define SpSessN         52
#define SptrialNperSess 49
#define SpSessHit       45
#define SpSessMiss      46
#define SpSessFA	    47
#define SpSessCR        48
#define SpSessCorrRate 	50

#define SpFirOdor       53
#define SpFirOdorL      54
#define SpOdorDelay     55
#define SpSecOdor       56
#define SpSecOdorL      57
#define Sptrialtype     58
#define SpITI           59  
#define SpproLpun       60
#define SpSess          61  
#define SpTrain         62  

#define Splaser         65
#define SpOdor_C        66
#define SpOdor_D        64
#define SpHit2         67
#define SpMiss2           68
#define SpFalseAlarm2         69
#define SpCorrectRejection2         70
#define SpHit3         71
#define SpMiss3          72
#define SpFalseAlarm3         73
#define SpCorrectRejection3        74




extern unsigned int timerCounterI, timerCounterJ;
extern char UartSend[4];
extern unsigned int ones, tens, hunds, thous, ten_thous;
extern unsigned int period;




void __attribute__((__interrupt__, no_auto_psv)) _T2Interrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _ADCInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _PWMInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _U2TXInterrupt(void);

void Digt_out(unsigned int Nchan, unsigned int Duration);


void Init_LCD(void);
void Init_ADC10(void);

void SetupPorts(void);
void SetupPorts(void);
void Init_PWM(void);
void Init_SPI(void);
void InitUART2(void);
void InitTMR(void);
void InitTMR1(void);

void Valve_ON(unsigned int N, unsigned int rate);
void Valve_OFF(unsigned int N);


void lcd_cmd(char cmd);
void lcd_data(char data);
void puts_lcd(unsigned char *data, unsigned char count);
void Mode_Show(unsigned int i);
void LCD_set_xy(unsigned int x, unsigned int y);

void Music_play(unsigned int i, unsigned int N);
void beep(void);
void Music_shut(void);

void Dispversion(int firstN, int secondN);

void NewDigt_out(unsigned int Nchan, float Duration);

void counts_dispose(unsigned int counter);

void DelayNmSec(unsigned int N);

void DelayNuSec(unsigned int N);
void DelayNmSecNew(unsigned int N);

void UART2PutChar(unsigned char Ch);
void Puts_UART2(unsigned char *data, unsigned char count);
void localSendTwice(int thisSp);
void localSendOnce(int thisSp, int thisValue);
void ErrorUartSend(void);

unsigned int getFuncNumber(int targetDigits, char* hint);
void LCDWriteString(char* s);

#endif	/* COMMONS_H */

